package com.wzy.business.store.login.presenter.impl

import android.content.Context
import android.util.Log
import com.wangzhenyu.common.base.BasePreImpl
import com.wangzhenyu.common.base.bean.LoginInfo
import com.wangzhenyu.common.util.log.LogUtil
import com.wzy.business.room.bean.User
import com.wzy.business.room.database.BusinessDatabase
import com.wzy.business.store.login.LoginActivity
import com.wzy.business.store.login.bean.CaptchaAli
import com.wzy.business.store.login.bean.LoginRequest
import com.wzy.business.store.login.presenter.LoginPresenter
import com.wzy.business.store.me.bean.AccountSetting
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.math.log

class LoginPresenterImpl(private var view: LoginPresenter.View) :
    BasePreImpl(view), LoginPresenter {

    override fun login(loginRequest: LoginRequest) {
        mCoroutine.launch(Dispatchers.IO) {
            var phone = ""
            phone = if (loginRequest.login_type == LoginActivity.LOGIN_BY_CODE) {
                loginRequest.mobile ?: ""
            } else {
                loginRequest.user_name ?: ""
            }
            (view as? Context)?.let {
                val loginInfoDao = BusinessDatabase.getInstance(it).getLoginInfoDao()

                val loginInfo = loginInfoDao.queryLoginInfoByPhone(phone)
                if (loginInfo.isEmpty()) {
                    view.onLoginError()
                } else {
                    view.onLoginSuccess(loginInfo[0])
                }
            }
        }

    }

    override fun register(phone: String, password: String, code: String, username: String) {
        //room
        mCoroutine.launch(Dispatchers.IO) {

            var loginInfo: LoginInfo? = null

            (view as? Context)?.let {
                val user = User(phone, password, code, username)
                val userDao = BusinessDatabase.getInstance(it).getUserDao()
                val loginInfoDao = BusinessDatabase.getInstance(it).getLoginInfoDao()
                //先查有没有
                val result = userDao.queryUserByPhone(phone)
                if (result.isNotEmpty()) {
                    //说明已经注册过了
                    loginInfo = loginInfoDao.queryLoginInfoByPhone(phone)[0]
                } else {
                    //没注册过
                    userDao.addUser(user)
                    //添加一个LoginInfo
                    loginInfoDao.addLoginInfo(LoginInfo().also { loginInfo ->
                        loginInfo.mobile = phone
                        loginInfo.shopId = try {
                            phone.substring(0, 3).toInt()
                        } catch (e: Exception) {
                            0
                        }


                    })
                    loginInfo = loginInfoDao.queryLoginInfoByPhone(phone)[0]
                }
            }

            view.onRegisterSuccess(loginInfo)

        }
    }

    //获取注册验证码
    override fun getRegisterSmsCode(bean: CaptchaAli) {
        view.onRegisterSmsCodeSuccess()
    }

    //获取登陆验证码
    override fun getLoginSmsCode(bean: CaptchaAli) {
        view.onLoginSmsCodeSuccess()

    }

    override fun requestAccountSettingData() {
        mCoroutine.launch {
            val bean = AccountSetting(
                "C店",
                false,
                "https://res.viayoo.com/v1/via-release-cn.apk",
                "2.0",
                false,
                "测试更新"
            )

            view.onAccountSettingSuccess(bean)


        }
    }


}